package iws.g5.uebung2.aufgabe2;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;

/**
 * Wolfram Universe 
 *
 * Copyright 2011, 2013 Kristian Kraljic
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at

 * http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * @author <a href=mailto:kristian@kra.lc>Kristian Kraljic</a>
 */
public class WolframFrame extends JFrame {
	private static final long serialVersionUID = 1L;

	public final WolframUniverse universe;
	
	public static void main(String[] args) {
		new WolframFrame().setVisible(true);
	}
	
	public WolframFrame() {
		this.universe = new WolframUniverse();
		universe.extinguish();
		boolean[] bigbang = new boolean[universe.SIZE];
		bigbang[universe.SIZE-1] = true;
		universe.compute(bigbang);
		
		((JPanel)this.getContentPane()).setBorder(new EmptyBorder(10,10,10,10));
		final JScrollPane scroll = new JScrollPane(this.universe);
		this.setLayout(new BorderLayout(10,10));
		this.add(scroll,BorderLayout.CENTER);
		
		JButton extinguish = new JButton("Extinguish this universe and create another big bang.");
		this.add(extinguish,BorderLayout.NORTH);
		extinguish.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionevent) {
				universe.extinguish();
				universe.compute();
				scroll.revalidate();
				setTitle();
			}
		});
		
		this.setTitle();
		this.setLocationByPlatform(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.pack();
		final int width = this.getSize().width+15;
		this.setMinimumSize(new Dimension(width,500));
		this.setSize(this.getMinimumSize());
		this.addComponentListener(new ComponentAdapter() {
			@Override public void componentResized(ComponentEvent event) {
				setSize(new Dimension(width,getHeight()));
			}
		});
	}
	
	public void setTitle() {
		this.setTitle("WolframUniverse (Idea: Stephen Wolfram, Implementation: Kristian Kraljic), Universe of "+(universe.age()!=universe.LIFESPAN?"age "+String.valueOf(universe.age()):"infinite age"));
	}
}
